package D12_6z.反射和Properties_3;

import java.io.FileReader;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.Properties;

public class Demo1 {
    public static void main(String[] args) throws Exception {
        Class<?> aClass = Class.forName("Y2019.M12_.D12_6z.反射和Properties_3.User");
        Method setUid = aClass.getDeclaredMethod("setUid", String.class);
        Method setUsername = aClass.getDeclaredMethod("setUsername", String.class);
        Method setPassword = aClass.getDeclaredMethod("setPassword", String.class);
        setUid.setAccessible(true);
        setUsername.setAccessible(true);
        setPassword.setAccessible(true);
        Object o = aClass.newInstance();
        Properties prop = new Properties();
        FileReader fr = new FileReader("src/Y2019/M12_/D12_6z/反射和Properties_3/data.properties");
        prop.load(fr);
        fr.close();
        for (Map.Entry<Object, Object> entry : prop.entrySet()) {
            if (entry.getKey().equals("uid")){
                setUid.invoke(o,entry.getValue());
            }
            if (entry.getKey().equals("username")){
                setUsername.invoke(o,entry.getValue());
            }
            if (entry.getKey().equals("password")){
                setPassword.invoke(o,entry.getValue());
            }
        }
        Method toString = aClass.getDeclaredMethod("toString");
        System.out.println(toString.invoke(o,null));
    }
}
